package com.southwind.controller;

import com.southwind.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/view")
//@SessionAttributes(value = "user")  自动加入到session
public class ViewHandler {

    @RequestMapping("/map")
    public String map(Map<String, User> map){
        User user = new User();
        user.setId(1L);
        user.setName("张三");
        map.put("user",user);
        return "view";
    }

    @RequestMapping("/model")
    public String model(Model model){
        User user = new User();
        user.setId(1L);
        user.setName("张三");
        model.addAttribute("user",user);
        return "view";
    }

    @RequestMapping("/modelAndView")
    public ModelAndView modelAndView(){
        User user = new User();
        user.setId(1L);
        user.setName("张三");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",user);
        modelAndView.setViewName("view");
        return modelAndView;
    }

    @RequestMapping("/modelAndView2")
    public ModelAndView modelAndView2(){
        User user = new User();
        user.setId(2L);
        user.setName("李四");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",user);
        View view = new InternalResourceView("/view.jsp");
        modelAndView.setView(view);
        return modelAndView;
    }

    @RequestMapping("/modelAndView3")
    public ModelAndView modelAndView3(){
        User user = new User();
        user.setId(3L);
        user.setName("王五");
        ModelAndView modelAndView = new ModelAndView("view");
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @RequestMapping("/modelAndView4")
    public ModelAndView modelAndView4(){
        User user = new User();
        user.setId(4L);
        user.setName("麻子");
        Map<String,User> map = new HashMap<>();
        map.put("user",user);
        ModelAndView modelAndView = new ModelAndView("view",map);
        return modelAndView;
    }

    @RequestMapping("/modelAndView5")
    public ModelAndView modelAndView5(){
        User user = new User();
        user.setId(5L);
        user.setName("乾哥");
        Map<String,User> map = new HashMap<>();
        map.put("user",user);
        View view = new InternalResourceView("/view.jsp");
        ModelAndView modelAndView = new ModelAndView(view,map);
        return modelAndView;
    }

    @RequestMapping("/modelAndView6")
    public ModelAndView modelAndView6(){
        User user = new User();
        user.setId(6L);
        user.setName("小乾哥");
        ModelAndView modelAndView = new ModelAndView("view","user",user);
        return modelAndView;
    }

    @RequestMapping("/request")
    public String request(HttpServletRequest request){
        User user = new User();
        user.setId(6L);
        user.setName("小乾哥");
        request.setAttribute("user",user);
        return "view";
    }

    @ModelAttribute
    public User getUser(){
        User user = new User();
        user.setId(6L);
        user.setName("小乾哥");
        return user;
    }

    @RequestMapping("/modelAttribute")
    public String modelAttribute(){
        return "view";
    }
}
